#include <irrlicht.h>
#include "main.h"
using namespace irr;
using namespace core;

class CSEditGUI
{
    private:
        IGUIEnvironment* env;
        IGUIElement * root;
        bool uvresize;
        f32 uvm;
        u32 resx,resy;

        IGUIStaticText * ge_uvgrid;
        IGUIStaticText * ge_surfs;
        IGUIStaticText * ge_pos;


        void buildGui()
        {
            int ButHeight=16;
            int ButLength=50;

            IGUISkin* skin = env->getSkin();

            for (s32 i=0; i<gui::EGDC_COUNT ; ++i)
            {
                video::SColor col = skin->getColor((gui::EGUI_DEFAULT_COLOR)i);
                col.setAlpha(255);
                skin->setColor((gui::EGUI_DEFAULT_COLOR)i, col);
            }

            env->addButton(rect<s32>(10,10,60,26), 0, 101, L"Open", L"Opens a file");
            env->addButton(rect<s32>(70,10,120,26), 0, 102, L"Close", L"Close current file");
            env->addButton(rect<s32>(130,10,180,26), 0, 103, L"Save", L"Saves the changes to the file");
            env->addButton(rect<s32>(190,10,240,26), 0, 199, L"Quit", L"Exits Program");

            f32 a = 0.3, b=0.5;

            ge_surfs  = env->addStaticText(L"", rect<s32>(0, resy-15, resx*a, resy),1,0,0,302,1);
            ge_uvgrid = env->addStaticText(L"\tMove, Grid size: 0.01",rect<s32>(resx*a, resy-15, resx*b, resy),1,0,0,301,1);
            ge_pos    = env->addStaticText(L"",rect<s32>(resx*b,resy-15,resx,resy),1,0,0,303,1);

            IGUIFont* font = env->getFont("fonts/font.xml"); //load font from swat3 :)
            skin->setFont(font);
            /*if (font)
            {
               ge_uvgrid->setOverrideFont(font);
               ge_redsurfs->setOverrideFont(font);
               ge_bluesurfs->setOverrideFont(font);
            }*/



        }
        void uvgrid_update()
        {
            wchar_t text[128];
            wchar_t op[32];

            if (uvresize)
                swprintf(op,L"Resize");
            else
                swprintf(op,L"Move");

            swprintf(text,L"\t%s. Grid size: %4.3f",op,uvm);

            root->getElementFromId(301)->setText(text);
        }

    public:
        CSEditGUI(IrrlichtDevice * device, bool r, f32 m)
        {
            env = device->getGUIEnvironment();
            root = env->getRootGUIElement();
            uvresize = r;
            uvm = m;
            dimension2d<u32> res = device->getVideoDriver()->getCurrentRenderTargetSize();
            resx = res.Width;
            resy = res.Height;
            buildGui();

        }
        IGUIEnvironment * getIrrGUIEnv()
        {
            return env;
        }
        //old version, displays selected surfaces indexes
        /*void surfs_update(CSelector * sel)
        {
            if (!sel) return;

            //update red, blue surfs text ***********************************
            wchar_t text1[256] = L"Selected Surfs";
            wchar_t text2[256] = L"Shared Surfs";
            array<u32>& rs = sel->getRedSurfs();
            array<u32>& bs = sel->getBlueSurfs();

            swprintf(text1,L"%ls (%d):",text1,rs.size());
            swprintf(text2,L"%ls (%d):",text2,bs.size());

            for (u32 i=0; i<rs.size(); i++)
                swprintf(text1,L"%ls %d", text1, rs[i]);
            for (u32 i=0; i<bs.size(); i++)
                swprintf(text2,L"%ls %d", text2, bs[i]);

            ge_redsurfs->setText(text1);
            ge_bluesurfs->setText(text2);
        }*/
        void surfs_update(CSelector * sel)
        {
            if (!sel) return;

            wchar_t text[128];
            swprintf(text,L"\tSurfaces: Selected (%d) Shared (%d)",sel->getRedSurfs().size(),sel->getBlueSurfs().size());
            ge_surfs->setText(text);
        }


        void uvgrid_setMult(f32 m)
        {
            uvm = m;
            uvgrid_update();
        }
        void uvgrid_setOp(bool r)
        {
            uvresize = r;
            uvgrid_update();
        }

        void pos_update(f32 x, f32 y, f32 z, const char * cellname)
        {
            wchar_t text[128];
            wchar_t wcellname[32];
            mbstowcs(wcellname, cellname,32);
            swprintf(text,L"% 10.3f, % 10.3f, % 10.3f, @ %s",x,y,z,wcellname);
            ge_pos->setText(text);
        }
};


